#! /bin/bash

####################################################################################################
# 文    件：create.sh
# 作    者: zzsvic@163.com
# 日    期：2023.06.10
# 说    明：
#     本脚本基于NXP的I.MX6ULL-EVK 板子的uboot和linux创建用户的板级配置
####################################################################################################

COLOR_R="\033[0;31m"
COLOR_G="\033[0;32m"
COLOR_Y="\033[0;33m"
COLOR_E="\033[m"
mydir=$PWD

function usage() {
    echo -e "${COLOR_Y}警告：传入参数错误\n${COLOR_E}"
    echo -e "${COLOR_G}用法: createboard.sh {board} <emmc|nand>${COLOR_E}"
}

####################################################################################################
# 解析传入参数
####################################################################################################
function getopts() {
    if [ $# -ne 2 ]; then usage; exit 1; fi
    BOARD=$1; shift;
    FLASH=$1; shift;

    echo -e "$COLOR_G"
    echo -e "######################################################################################"
    echo -e "# 本脚本基于NXP的I.MX6ULL-EVK 板子的uboot和linux创建用户的板级配置"
    echo -e "# 新的板子：$BOARD"
    echo -e "# 存储类型：$FLASH"
    echo -e "######################################################################################"
    echo -e "$COLOR_E"
}

####################################################################################################
# uboot创建新板子
####################################################################################################
function uboot_create_board() {
    local DSRC=${mydir}/custom/uboot-imx

    # Step 1: 创建默认板级配置文件
    cp -rf ${DSRC}/configs/mx6ull_14x14_evk_${FLASH}_defconfig  ${DSRC}/configs/mx6ull_14x14_${BOARD}_${FLASH}_defconfig
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/configs/mx6ull_14x14_${BOARD}_${FLASH}_defconfig
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/configs/mx6ull_14x14_${BOARD}_${FLASH}_defconfig
    sed -i -e "s/mx6ull${BOARD}/mx6ull_${BOARD}/g"              ${DSRC}/configs/mx6ull_14x14_${BOARD}_${FLASH}_defconfig

    cp -rf ${DSRC}/configs/mx6ull_14x14_evk_plugin_defconfig    ${DSRC}/configs/mx6ull_14x14_${BOARD}_plugin_defconfig
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/configs/mx6ull_14x14_${BOARD}_plugin_defconfig
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/configs/mx6ull_14x14_${BOARD}_plugin_defconfig
    sed -i -e "s/mx6ull${BOARD}/mx6ull_${TARGET}/g"             ${DSRC}/configs/mx6ull_14x14_${BOARD}_plugin_defconfig

    cp -rf ${DSRC}/configs/mx6ulz_14x14_evk_defconfig           ${DSRC}/configs/mx6ulz_14x14_${BOARD}_defconfig
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/configs/mx6ulz_14x14_${BOARD}_defconfig
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/configs/mx6ulz_14x14_${BOARD}_defconfig
    sed -i -e "s/mx6ull${BOARD}/mx6ull_${BOARD}/g"              ${DSRC}/configs/mx6ulz_14x14_${BOARD}_defconfig

    # Step 2: 创建开发板头文件  
    cp -rf ${DSRC}/include/configs/mx6ullevk.h                  ${DSRC}/include/configs/mx6ull_${BOARD}.h
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/include/configs/mx6ull_${BOARD}.h
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/include/configs/mx6ull_${BOARD}.h
    sed -i -e "s/MX6ULL${BOARD^^}/MX6ULL_${BOARD^^}/g"          ${DSRC}/include/configs/mx6ull_${BOARD}.h

    # Step 3: 创建开发板板级文件    
    cp -rf ${DSRC}/board/freescale/mx6ullevk                    ${DSRC}/board/freescale/mx6ull_${BOARD}
    mv ${DSRC}/board/freescale/mx6ull_${BOARD}/mx6ullevk.c      ${DSRC}/board/freescale/mx6ull_${BOARD}/mx6ull_${BOARD}.c
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/board/freescale/mx6ull_${BOARD}/*
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/board/freescale/mx6ull_${BOARD}/*
    sed -i -e "s/mx6ull${BOARD}/mx6ull_${BOARD}/g"              ${DSRC}/board/freescale/mx6ull_${BOARD}/*
    sed -i -e "s/MX6ULL${BOARD^^}/MX6ULL_${BOARD^^}/g"          ${DSRC}/board/freescale/mx6ull_${BOARD}/*

    # Step 4: 创建开发板设备树
    cp -rf ${DSRC}/arch/arm/dts/imx6ull-14x14-evk-${FLASH}.dts  ${DSRC}/arch/arm/dts/imx6ull-14x14-${BOARD}-${FLASH}.dts
    cp -rf ${DSRC}/arch/arm/dts/imx6ull-14x14-evk.dts           ${DSRC}/arch/arm/dts/imx6ull-14x14-${BOARD}.dts
    cp -rf ${DSRC}/arch/arm/dts/imx6ul-14x14-evk-u-boot.dtsi    ${DSRC}/arch/arm/dts/imx6ul-14x14-${BOARD}-u-boot.dtsi
    cp -rf ${DSRC}/arch/arm/dts/imx6ul-14x14-evk.dtsi           ${DSRC}/arch/arm/dts/imx6ul-14x14-${BOARD}.dtsi
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/arch/arm/dts/imx6ul*14x14*${BOARD}*.dts*
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/arch/arm/dts/imx6ul*14x14*${BOARD}*.dts*

    local LINE=`cat ${DSRC}/arch/arm/dts/Makefile|grep -n imx6ull-14x14-evk-${FLASH}.dtb|awk -F ":" '{print $1}'`
    sed -i -e ''$LINE'a \\timx6ull-14x14-'${BOARD}'-'${FLASH}'.dtb \\' ${DSRC}/arch/arm/dts/Makefile

    # Step 5: mach-imx 添加新配置
    local LINE=`cat ${DSRC}/arch/arm/mach-imx/mx6/Kconfig|grep -n TARGET_MX6ULL_14X14_EVK|awk -F ":" '{print $1}'`
    sed -i -e ''$LINE'i \\timply CMD_DM\n'                              ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tselect OF_SYSTEM_SETUP'                      ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tselect IMX_MODULE_FUSE'                      ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tselect DM_THERMAL'                           ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tselect DM'                                   ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tselect BOARD_LATE_INIT'                      ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tdepends on MX6ULL'                           ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i \\tbool \"Support mx6ull_14x14_'${BOARD}'\"'    ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
    sed -i -e ''$LINE'i config TARGET_MX6ULL_14X14_'${BOARD^^}''        ${DSRC}/arch/arm/mach-imx/mx6/Kconfig

    local LINE=`cat ${DSRC}/arch/arm/mach-imx/mx6/Kconfig|grep -n mx6ullevk|awk -F ":" '{print $1}'`
    sed -i -e ''$LINE'i source \"board/freescale/mx6ull_'${BOARD}'/Kconfig\"' ${DSRC}/arch/arm/mach-imx/mx6/Kconfig
}

####################################################################################################
# linux创建新板子
####################################################################################################
function linux_create_board() {
    local DSRC=${mydir}/custom/linux-imx

    cp -rf ${DSRC}/arch/arm/configs/imx_v7_defconfig            ${DSRC}/arch/arm/configs/imx_${BOARD}_${FLASH}_defconfig
    cp -rf ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-evk-emmc.dts ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-${BOARD}-${FLASH}.dts
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-${BOARD}-${FLASH}.dts
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-${BOARD}-${FLASH}.dts

    cp -rf ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-evk.dts      ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-${BOARD}.dts
    sed -i -e "s/evk/${BOARD}/g"                                ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-${BOARD}.dts
    sed -i -e "s/EVK/${BOARD^^}/g"                              ${DSRC}/arch/arm/boot/dts/imx6ull-14x14-${BOARD}.dts

    cp -rf ${DSRC}/arch/arm/boot/dts/imx6ul-14x14-evk.dtsi      ${DSRC}/arch/arm/boot/dts/imx6ul-14x14-${BOARD}.dtsi
    # sed -i -e "s/evk/${BOARD}/g"                              ${DSRC}/arch/arm/boot/dts/imx6ul-14x14-${BOARD}.dtsi
    # sed -i -e "s/EVK/${BOARD^^}/g"                            ${DSRC}/arch/arm/boot/dts/imx6ul-14x14-${BOARD}.dtsi

    local LINE=`cat ${DSRC}/arch/arm/boot/dts/Makefile|grep -n imx6ull-14x14-evk-${FLASH}.dtb|awk -F ":" '{print $1}'`
    sed -i -e ''$LINE'a \\timx6ull-14x14-'${BOARD}'-'${FLASH}'.dtb \\'  ${DSRC}/arch/arm/boot/dts/Makefile
}

####################################################################################################
# 命令执行
####################################################################################################
#set -xv
getopts $*
uboot_create_board
linux_create_board
##end
